

//--------------------------------------------------------------------------------------------------------------------
/// when loading, add the hidden div
function create_hidden_waiting_div( path_to_gif )
{
    var nGif = 6;// if the number of gif change, change this number
    var randGif = Math.floor((Math.random()*nGif)+1); 
    $("body").append('<div id="waitbox_id" class="waitbox">');
    $("#waitbox_id").append('<img src="'+path_to_gif+'images/loading'+randGif+'.gif" alt="Please wait..." border="0" align="top"  style="width:70px;" />');
    return true;
};


//--------------------------------------------------------------------------------------------------------------------
// take set the visibility of the specified element accordingly to the flag 
function ap_showWaitMessage(flag, STATIC_URL)
{
    if ($("#waitbox_id").length == 0)
    {
         create_hidden_waiting_div( STATIC_URL );
    }
    if( flag )
    {   
        $("#waitbox_id").show();
        $("#waitbox_id").css("position","absolute");
        $("#waitbox_id").css("top", Math.max(0, (($(window).height() - $("#waitbox_id").outerHeight()) / 2) +  $(window).scrollTop()) + "px");
        $("#waitbox_id").css("left", Math.max(0, (($(window).width() - $("#waitbox_id").outerWidth()) / 2) +   $(window).scrollLeft()) + "px");
    }
    else
    {
         $("#waitbox_id").hide();
    }
    return true;
}

//--------------------------------------------------------------------------------------
// post comment on enter pressed -->
function ifEnterArg(field,event, functionToCall, argStr )
{    
    var theCode = (event.keyCode ? event.keyCode : (event.which ? event.which : event.charCode));
    if (theCode == 13)
    {   
        if( argStr == '' )
        {
            functionToCall();
        }
        else
        {
            functionToCall(argStr);
        }
        return false;
    }
    else
    return true;
};

//--------------------------------------------------------------------------------------
// post comment on enter pressed -->
function ifEnter(field,event, functionToCall )
{
    return ifEnterArg(field,event,functionToCall,'');
};

//--------------------------------------------------------------------------------------------------------------------
// submit the form
function submitForm(formName)
{   
    tmp = document.forms[formName]
    tmp.submit(); 
}

//--------------------------------------------------------------------------------------------------------------------
// submit the request displaying the waiting gif.
// In case of success with 0 response, alert the success message when not empty and reload
// In case of success with not 0 response, load the return
// in case of error, load the error message, should not happen
function submitWithReload(theUrl, theData, theSuccessMessage ) 
{
    ap_showWaitMessage(true, '');  
	var args = { 
               type: "POST", 
               url: String(theUrl),
               cache: false,
               data: theData, 
               complete:function(res)
                        {                     
                            if( res.responseText == '0' )
                            {
                                if( theSuccessMessage!= '' )
                                {
                                    alert( theSuccessMessage );
                                }
                                window.location.reload();
                            }
                            else
                            {
                             document.write( res.responseText );
                            }
                        },
               error: function(errorThrown)  { document.write(errorThrown.responseText);  }
               };
    $.ajax(args);
	return false;
};

//--------------------------------------------------------------------------------------
// fill paramObj with the name/value of the elements of elementForData with type inputType 
function fillWith( paramObj, elementForData, inputType )
{
    elementForData.find(inputType).each( 
        function ( index, domEle ) 
            {
                  paramObj[$(domEle).attr("name")] = $(domEle).val();
            }
        );
}


//--------------------------------------------------------------------------------------
// submit a a dictionair from the the 'input', 'select', and 'textarea' divs in elementForData to the specified url
// in case of success it print the msg and reload the jsons
// in case the div where the message is printed is created when loading the jsons, the success message is also kept in
// optionalMemoryDiv_id when passed to have persistent data. (the devoleper can do whatever he wants with this data)
function submitWithoutReload( url, elementForData, elementForMsg_id, theSuccessMessage, optionalCallbackFunc ) 
{ 
    var paramObj = {};
    //get the data from the input in the table
    fillWith( paramObj, elementForData, "input" );
    fillWith( paramObj, elementForData, "select" ); 
    fillWith( paramObj, elementForData, "textarea" ); 

    var args = { 
            type: "POST", 
            url: url, 
            cache: false, 
            data: paramObj, 
            complete:function(res)
                        {    
                            ap_showWaitMessage(false, '');
                            $(elementForMsg_id).show();
                            var respTxt = res.responseText;          
                            if( respTxt == '0' )
                            {
                                // display the success message
                                if( $(elementForMsg_id).length > 0 ){
                                    $(elementForMsg_id).text( theSuccessMessage );
                                    setTimeout( function() { $(elementForMsg_id).fadeOut(); }, 3000 );
                                }
                                // reload the json (but not the page)
                                return callJsonsFromPythonThenApplyJSFuncToThem(optionalCallbackFunc);
                            }
                            else
                            {
                                $(elementForMsg_id).text(respTxt);
                                return false;
                            }
                        },
           error: function(errorThrown)  { document.write(errorThrown.responseText);  }
           };
    var pathToGif = "";
    if( typeof(STATIC_URL_JS) != 'undefined' )
    {
        pathToGif = STATIC_URL_JS;
    }
    ap_showWaitMessage(true,  pathToGif );
    $.ajax(args);
    return false;
};



